package com.caigou.service.impl;

import com.caigou.bean.cms.entity.Article;
import com.caigou.bean.cms.entity.ArticleInformation;
import com.caigou.bean.cms.entity.CollectionConfig;
import com.caigou.bean.cms.entity.CollectionTimeConfig;
import com.caigou.bean.cms.param.CollectionParam;
import com.caigou.component.RabbitProducer;
import com.caigou.enums.ResponseEnum;
import com.caigou.mapper.cms.ArticleMapper;
import com.caigou.mapper.cms.CollectionMapper;
import com.caigou.pojo.CollectionList;
import com.caigou.pojo.DictionaryList;
import com.caigou.repository.EsArticleRepository;
import com.caigou.service.*;
import com.caigou.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Resource
    private RabbitProducer rabbitProducer;

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleAdjunctService articleAdjunctService;
    @Resource
    private ArticleInformationService articleInformationService;
    @Resource
    private ArticleLawService articleLawService;
    @Resource
    private ArticleLawEntryService articleLawEntryService;
    @Resource
    private ArticleTitleService articleTitleService;
    @Resource
    private ChannelDataService channelDataService;

    @Resource
    private EsArticleRepository esArticleRepository;

    @Resource
    private CollectionMapper collectionMapper;

    @Override
    public Result loadCollectionList(CollectionParam param) {
        List<CollectionList> list = collectionMapper.loadCollectionList(param.getTitle(),param.getStatus());
        list.forEach(item-> item.setCollectionTimeConfigList(collectionMapper.loadCollectionTimeConfigList(item.getId())));
        return Result.success(list);
    }

    @Override
    public Result saveCollectionTimeConfig(CollectionParam param) {
        // 相同数据验证
        int result = collectionMapper.loadCollectionTimeConfig(param.getCollectionId(),param.getTime());
        if (result > 0) return Result.error(ResponseEnum.COLLECTION_TIME_SAVE_ERROR);
        CollectionTimeConfig timeConfig = new CollectionTimeConfig();
        timeConfig.setCollectionId(param.getCollectionId());
        timeConfig.setCollectionTime(param.getTime());
        collectionMapper.saveCollectionTimeConfig(timeConfig);
        // 修改了启动时间配置--发送MQ消息给爬虫
        CollectionConfig config = collectionMapper.loadCollectionConfig(param.getCollectionId());
        Map<String, Object> map = new HashMap<>();
        map.put("crawler_changed", param.getCollectionId());
        rabbitProducer.sendCollectionChangeMessage(map,config.getMqQueueName());
        return Result.success(timeConfig);
    }

    @Override
    public Result deleteCollectionTimeConfig(CollectionParam param) {
        // 删除当前时间配置
        int result = collectionMapper.deleteCollectionTimeConfig(param.getId());
        if (result > 0){
            // 删除完成之后，如果当前采集源已经没有时间配置，则自动设置成关闭状态
            List<CollectionTimeConfig> list = collectionMapper.loadCollectionTimeConfigList(param.getCollectionId());
            if (null == list){
                collectionMapper.updateCollectionStatus(param.getCollectionId(),0);
            }
        }
        // 修改了启动时间配置--发送MQ消息给爬虫
        CollectionConfig config = collectionMapper.loadCollectionConfig(param.getCollectionId());
        Map<String, Object> map = new HashMap<>();
        map.put("crawler_changed", param.getCollectionId());
        rabbitProducer.sendCollectionChangeMessage(map,config.getMqQueueName());
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result updateCollectionStatus(CollectionParam param) {
        if (param.getUpdateType() == 1){// 设置启用状态
            if (param.getStatus() == 1){
                // 如果没有时间配置则无法启用
                List<CollectionTimeConfig> list = collectionMapper.loadCollectionTimeConfigList(param.getCollectionId());
                if (null == list) return Result.error(ResponseEnum.COLLECTION_TIME_ERROR);
            }
            collectionMapper.updateCollectionStatus(param.getCollectionId(),param.getStatus());
        } else {
            collectionMapper.updateCollectionErrorStatus(param.getCollectionId(),param.getStatus());
        }

        // 修改了启动时间配置--发送MQ消息给爬虫
        CollectionConfig config = collectionMapper.loadCollectionConfig(param.getCollectionId());
        Map<String, Object> map = new HashMap<>();
        map.put("crawler_changed", param.getCollectionId());
        rabbitProducer.sendCollectionChangeMessage(map,config.getMqQueueName());
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result updateCollectionErrorConfig(CollectionParam param) {
        collectionMapper.updateCollectionErrorConfig(param.getCollectionId(),param.getCollectionDay(),param.getCollectionCount());

        // 修改了启动时间配置--发送MQ消息给爬虫
        CollectionConfig config = collectionMapper.loadCollectionConfig(param.getCollectionId());
        Map<String, Object> map = new HashMap<>();
        map.put("crawler_changed", param.getCollectionId());
        rabbitProducer.sendCollectionChangeMessage(map,config.getMqQueueName());
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public List<DictionaryList> loadCollectionListDictionary() {
        return collectionMapper.loadCollectionListDictionary();
    }

    @Override
    public Result loadCollectionCountInfo() {
        int allCount = collectionMapper.loadCollectionAllCount();
        int errorCount = collectionMapper.loadCollectionErrorCount();
        Map<String,Integer> map = new HashMap<>();
        map.put("allCount", allCount);
        map.put("errorCount", errorCount);
        return Result.success(map);
    }

    @Override
    public Result deleteSubstationData(CollectionParam param) {
        List<Article> list = articleService.loadArticleListBySubstation(param.getSubstationId());
        list.forEach(item ->{
            // 删除附件表
            articleAdjunctService.deleteArticleAdjunctByArticleId(item.getArticleId());
            // 删除详情表
            articleInformationService.deleteArticleInformationData(item.getArticleId());
            // 删除法律表
            articleLawService.deleteArticleLawData(item.getArticleId());
            // 删除法律字表
            articleLawEntryService.deleteArticleEntryByArticleId(item.getArticleId());
            // 删除标题表
            articleTitleService.deleteArticleTitleByArticleId(item.getArticleId());
            // 删除频道表
            channelDataService.deleteArticle(item.getArticleId());
            // 删除文章主表
            articleService.deleteArticle(item.getArticleId());
            // 发送ES清除数据
            esArticleRepository.deleteById(item.getArticleId());
        });
        return Result.success(ResponseEnum.SUCCESS);
    }
}
